<?php

require_once getcwd().'/data/IRepository.php';
require_once getcwd().'/data.models/UserRepo.php';
require_once getcwd().'/data.impl/ShotDayRepository.php';

$_ENV['connectionParameter'] = array(
		'engine' => 'mysql',
		'host' => 'localhost',
		'user' => 'root',
		'password' => '' ,
		'dbname' =>'applicationn_sotd'
);

class SearchShotDayRepository extends ShotDayRepository {
	

	public function getShotsDayByTag($tag){
		$vardate = date("d/m/Y-H:i:s");
		$sql="select * from sotd_shotday where key_first like '%" . $tag. "%' OR key_snd like '%" . $tag. "%' OR key_third like '%" . $tag. "%';";
		error_log($vardate ." - SearchShotDayRepository - get : " .$sql . "\n" , 3, "log/mes-erreurs.log");
		try {
			$connection = $_ENV['connectionParameter'];
			$dsn = $connection['engine'] .":dbname=" .$connection['dbname'] . ";host=". $connection['host'];
			$pdo = new PDO($dsn,$connection['user'],$connection['password']);
			$pdoStatement = $pdo->query($sql);
			//echo "Type : ". gettype($pdoStatement). "<br />";
			if($pdoStatement ==false)
				return false;
			else {				
				$postList = array();
				foreach($pdoStatement as $row){
					$post = new ShotDayRepo();
					$post->imgid=$row['imgid'];
					$post->src=$row['src'];
					$post->description=$row['description'];
					$post->city=$row['city'];
					$post->country=$row['country'];
					$post->date=$row['date'];
					$post->useruid=$row['useruid'];
					$post->tag_first= $row['key_first'];
					$post->tag_second = $row['key_snd'];
					$post->tag_third = $row['key_third'];
					$postList[]=$post;
						
				}
		
				$pdoStatement->closeCursor();
				unset($pdoStatement);
				unset($pdo);
				unset($connection);
				return $postList;
		
			}
				
				
		} catch (PDOException $e) {
			echo $e->getMessage();
			return false;
		}
	}
		
		public function getShotsDayUserByTag($tag,$id){
			$vardate = date("d/m/Y-H:i:s");
			$sql="select * from sotd_shotday where useruid = '". $id ."' and key_first like '%" . $tag. "%' OR key_snd like '%" . $tag. "%' OR key_third like '%" . $tag. "%';";
			error_log($vardate ." - SearchShotDayRepository - get : " .$sql . "\n" , 3, "log/mes-erreurs.log");
			try {
				$connection = $_ENV['connectionParameter'];
				$dsn = $connection['engine'] .":dbname=" .$connection['dbname'] . ";host=". $connection['host'];
				$pdo = new PDO($dsn,$connection['user'],$connection['password']);
				$pdoStatement = $pdo->query($sql);
				//echo "Type : ". gettype($pdoStatement). "<br />";
				if($pdoStatement ==false)
					return false;
				else {
					$post = new ShotDayRepo();
					$postList = array();
					foreach($pdoStatement as $row){
						$post->imgid=$row['imgid'];
						$post->src=$row['src'];
						$post->description=$row['description'];
						$post->city=$row['city'];
						$post->country=$row['country'];
						$post->date=$row['date'];
						$post->useruid=$row['useruid'];
						$post->tag_first= $row['key_first'];
						$post->tag_second = $row['key_snd'];
						$post->tag_third = $row['key_third'];
						$postList[]=$post;
		
					}
		
					$pdoStatement->closeCursor();
					unset($pdoStatement);
					unset($pdo);
					unset($connection);
					return $postList;
		
				}
		
		
			} catch (PDOException $e) {
				echo $e->getMessage();
				return false;
			}
		
	}
	
	
	
	
}
